package com.yinxin.pool;

import com.yinxin.util.ThreadUtil;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @author yinxin
 * @Title:
 * @Package
 * @Description:
 * @date 2022/1/1016:48
 */
public class TestPool {

    public static void main(String[] args) {
        //1、创建服务，创建线程池
        //newFixedThreadPool 参数为：线程池大小
        ExecutorService service= Executors.newFixedThreadPool(10);

        service.execute(new MyThread());
        service.execute(new MyThread());
        service.execute(new MyThread());
        service.execute(new MyThread());
        //2、关闭连接
        service.shutdown();

        /**
         * 提交callable用submit
         * 提交runnable用execute
         * void shutdown():关闭连接池
         * Executors：工具类、线程池的工厂类，用于创建并返回不同类型的线程池
         */
    }
}

class  MyThread implements Runnable{
    @Override
    public void run() {
        for (int i = 0; i < 100; i++) {
            System.out.println(Thread.currentThread().getName()+i);
            ThreadUtil.sleep(1000);
        }
    }
}